using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;

namespace Noein.Control
{
    /// <summary>
    /// Spawn given set of objects around the target object.
    /// </summary>
    //class ParticleSystemController : Controller
    //{
    //    private class Particle
    //    {
    //        private Object obj;
    //        public Object Object
    //        {
    //            get { return obj; }
    //            set { obj = value; }
    //        }

    //        private float lifeTime;
    //        public float LifeTime
    //        {
    //            get { return lifeTime; }
    //            set { lifeTime = value; }
    //        }

    //        private float age;
    //        public float Age
    //        {
    //            get { return age; }
    //            set { age = value; }
    //        }
    //    }

    //    private float lifeTime;
    //    private float rate;
    //    private float angle;
    //    private float spread;
    //    private float randomness;
    //    private float liveCount;

    //    private Random rand;
    //    private ParticleController pc;
    //    private LinkedList<Particle> particles;

    //    private float timeSinceLastSpawn = 0;

    //    public ParticleSystemController(float lifeTime, float randomness, float rate, params Object[] particles)
    //        : base()
    //    {
    //        this.lifeTime = lifeTime;
    //        this.randomness = randomness;
    //        this.rate = rate;
    //        this.liveCount = 0;

    //        rand = new Random();
    //        pc = new ParticleController();
    //        pc.Integrator = Math.NumericalIntegration.Euler;

    //        Particle[] particleArray = new Particle[particles.Length];
    //        for (int i = 0; i < particles.Length; i++)
    //        {
    //            particleArray[i] = new Particle();
    //            particleArray[i].Object = particles[i];
    //            //ResetParticle(particleArray[i]);
    //        }
    //        this.particles = new LinkedList<Particle>(particleArray);
    //    }

    //    public override void OnFrameMove(Microsoft.DirectX.Direct3D.Device device, double appTime, float elapsedTime)
    //    {
    //        // update live particles
    //        LinkedListNode<Particle> node = particles.First;
    //        int count = 0;
    //        do
    //        {
    //            LinkedListNode<Particle> nextNode = node.Next;

    //            Particle particle = node.Value;
    //            particle.Age += elapsedTime;

    //            if (particle.Age > particle.LifeTime)
    //            {
    //                // kill dead particles
    //                ResetParticle(particle);
    //                particles.Remove(node);
    //                particles.AddLast(node);
    //                liveCount--;
    //            }
    //            else
    //            {
    //                pc.Target = node.Value.Object;
    //                pc.OnFrameMove(device, appTime, elapsedTime);
    //                count++;
    //            }

    //            node = nextNode;
    //        } while (count < liveCount);

    //        // spawn new particles
    //        timeSinceLastSpawn += elapsedTime;
    //        int numToGen = (int)(timeSinceLastSpawn * rate);
    //        if (numToGen > 0) timeSinceLastSpawn = timeSinceLastSpawn % rate;
    //        while (numToGen > 0 && liveCount < particles.Count)
    //        {
    //            Particle particle = node.Value;
    //            particle.Object.Visual.Visible = true;
    //            numToGen--;
    //            liveCount++;
    //        }
    //    }

    //    public override void Reset()
    //    {
    //        foreach (Particle p in particles)
    //        {
    //            ResetParticle(p);
    //        }
    //    }

    //    private void ResetParticle(Particle particle)
    //    {
    //        particle.Object.Visual.Visible = false;
    //        particle.Age = 0;
    //        particle.LifeTime = lifeTime - lifeTime * (float)rand.NextDouble() * randomness;

    //        particle.Object.Spatial.TeleportTo(Target.Spatial.Position);

    //        particle.Object.Physical.Momentum = new Vector3(0, 1, 0);
    //    }
    //}
}
